package cn.xz.hfsk.util

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.widget.PopupWindow
import android.widget.SeekBar
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import cn.xz.hfsk.R
import cn.xz.hfsk.base.BaseApplication

/**
 * SeekBar移动时弹出对应的气泡加数字*/
@SuppressLint("StaticFieldLeak")
object SeekBarPopUtils {

    private var popWin: PopupWindow? = null
    private var clPopPar: ConstraintLayout? = null
    private var tvPopTxt: TextView? = null

    fun showPop(seekBar: SeekBar){
        popWin = PopupWindow()
        val mPopView = LayoutInflater.from(BaseApplication.getContext()).inflate(R.layout.item_popup_win,null,false)
        clPopPar = mPopView.findViewById<ConstraintLayout>(R.id.cl_pop_par)
        tvPopTxt = mPopView.findViewById<TextView>(R.id.tv_pop_txt)

        popWin?.contentView = mPopView
        popWin?.height = AppHelper.dp2px(30)
        popWin?.width = seekBar.width
        popWin?.showAsDropDown(seekBar,0,-(AppHelper.dp2px(30) + popWin!!.height))
    }


    fun move(progress: Int,seekBar: SeekBar,video: Boolean){
        if (clPopPar != null) {
            if (video) {
                val tvPopWidth: Int = AppHelper.dp2px(60)
                val params = ConstraintLayout.LayoutParams(
                    tvPopWidth, AppHelper.dp2px(30)
                )
                params.startToStart = clPopPar!!.id
                params.endToEnd = clPopPar!!.id
                tvPopTxt!!.layoutParams = params
                tvPopTxt!!.setText(TimeUtils.getTime(progress))
            } else {
                val tvPopWidth: Int = AppHelper.dp2px(40)
                val params = ConstraintLayout.LayoutParams(
                    tvPopWidth, AppHelper.dp2px(30)
                )
                params.startToStart = clPopPar!!.id
                params.marginStart =
                    (seekBar.width - tvPopWidth) / seekBar.max * progress + tvPopWidth / 3
                tvPopTxt!!.layoutParams = params
                tvPopTxt!!.text = progress.toString() + ""
            }
        }
    }


    fun dismiss(){
        popWin?.dismiss()
        popWin = null
        clPopPar = null
        tvPopTxt = null
    }

}